import time
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By


class BasePage:
    _chromeDriverPath = '../chromedriver.exe'
    _baseurl = None
    web_driver = None

    def __init__(self, driver=None):
        if driver:
            """
             非首次实例化，对self.web_driver赋值
            """
            self.web_driver = driver
        else:
            """
            首次实例化，增加cookies，进行登陆
            """
            options = webdriver.ChromeOptions()
            options.add_argument('window-size=1920x1080')  # 指定浏览器分辨率
            self.web_driver = webdriver.Chrome(executable_path=self._chromeDriverPath, chrome_options=options)
            self.web_driver.implicitly_wait(3)
            if self._baseurl is not None:
                self.web_driver.get(self._baseurl)
                with open("./cookie.yaml") as f:
                    fobj = yaml.safe_load(f)
                    for c in fobj:
                        self.web_driver.add_cookie(c)
                    time.sleep(3)
                    self.web_driver.get(self._baseurl)
                    f.close()
            else:
                pass

    def quit_program(self):
        self.web_driver.quit()

    def find_web_element(self, *args: str):
        """
        封装元素定位，找单个元素
        :param args:
        :return:
        """
        if str(args[0]) == 'CSS' or str(args[0]).lower() == 'css':
            return self.web_driver.find_element(By.CSS_SELECTOR, args[1])
        elif str(args[0]) == 'Name' or str(args[0]).lower() == 'name':
            return self.web_driver.find_element(By.NAME, args[1])
        elif str(args[0]) == 'ID' or str(args[0]).lower() == 'id':
            return self.web_driver.find_element(By.ID, args[1])
        elif str(args[0]) == 'TAG_NAME' or str(args[0]).lower() == 'tag_name':
            return self.web_driver.find_element(By.TAG_NAME, args[1])
        elif str(args[0]) == 'XPATH' or str(args[0]).lower() == 'xpath':
            return self.web_driver.find_element(By.XPATH, args[1])
        elif str(args[0]) == 'LINK_TEXT' or str(args[0]).lower() == 'link_text':
            return self.web_driver.find_element(By.LINK_TEXT, args[1])
        elif str(args[0]) == 'CLASS_NAME' or str(args[0]).lower() == 'class_name':
            return self.web_driver.find_element(By.CLASS_NAME, args[1])
        elif str(args[0]) == 'PARTIAL_LINK_TEXT' or str(args[0]).lower() == 'partial_link_text':
            return self.web_driver.find_element(By.PARTIAL_LINK_TEXT, args[1])
        else:
            raise TypeError("参数类型不正确")

    def find_web_elements(self, *args: str):
        """
        封装元素定位，找多个元素
        :param args:
        :return:
        """
        if str(args[0]) == 'CSS' or str(args[0]).lower() == 'css':
            return self.web_driver.find_elements(By.CSS_SELECTOR, args[1])
        elif str(args[0]) == 'Name' or str(args[0]).lower() == 'name':
            return self.web_driver.find_elements(By.NAME, args[1])
        elif str(args[0]) == 'ID' or str(args[0]).lower() == 'id':
            return self.web_driver.find_elements(By.ID, args[1])
        elif str(args[0]) == 'TAG_NAME' or str(args[0]).lower() == 'tag_name':
            return self.web_driver.find_elements(By.TAG_NAME, args[1])
        elif str(args[0]) == 'XPATH' or str(args[0]).lower() == 'xpath':
            return self.web_driver.find_elements(By.XPATH, args[1])
        elif str(args[0]) == 'LINK_TEXT' or str(args[0]).lower() == 'link_text':
            return self.web_driver.find_elements(By.LINK_TEXT, args[1])
        elif str(args[0]) == 'CLASS_NAME' or str(args[0]).lower() == 'class_name':
            return self.web_driver.find_elements(By.CLASS_NAME, args[1])
        elif str(args[0]) == 'PARTIAL_LINK_TEXT' or str(args[0]).lower() == 'partial_link_text':
            return self.web_driver.find_elements(By.PARTIAL_LINK_TEXT, args[1])
        else:
            raise TypeError("参数类型不正确")
